<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\MktFriend;

/**
 * MktFriendSearch represents the model behind the search form about `common\models\MktFriend`.
 */
class MktFriendSearch extends MktFriend
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'friend_id'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MktFriend::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'friend_id' => $this->friend_id,
        ]);

        return $dataProvider;
    }

    public function searchWap($params, $query = null)
    {
        if (null === $query) {
            $query = MktFriend::find()
/*
                ->andWhere (['>=', 'status', MktUser::STATUS_BASE_INFO])
                ->andWhere(['disabled' => 0])
                //->andWhere (['subscribe' => 1])
*/
                ->orderBy(['id' => SORT_DESC]);
        }

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'params' => $params,
                'pagesize' => 100,
                'validatePage' => false,
            ],
        ]);


        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'friend_id' => $this->friend_id,
        ]);

        return $dataProvider;
    }

}
